package com.jtpay.ysfproductionprocess.db;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.List;

import static com.jtpay.ysfproductionprocess.db.YSFConstant.*;

public class YSFContentProvider extends ContentProvider {
    private Context mContext;
    private SQLiteDatabase mDatabase;
    private static final UriMatcher sUriMatcher;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(YSFConstant._AUTHORITY, YSFEncryptColumn._TABLE_NAME, TYPE_ENCRYPT);
        sUriMatcher.addURI(YSFConstant._AUTHORITY, YSFEncryptColumn._TABLE_NAME + "/#", TYPE_ENCRYPT_ITEM);
        sUriMatcher.addURI(YSFConstant._AUTHORITY, YSFDecryptColumn._TABLE_NAME, TYPE_DECRYPT);
        sUriMatcher.addURI(YSFConstant._AUTHORITY, YSFDecryptColumn._TABLE_NAME + "/#", TYPE_DECRYPT_ITEM);
    }

    @Override
    public boolean onCreate() {
        mContext = getContext();
        YSFDBHelper mHelper = new YSFDBHelper(mContext);
        mDatabase = mHelper.getWritableDatabase();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ENCRYPT_ITEM:
            case TYPE_DECRYPT_ITEM:
                String id = path.get(1);
                selection = YSFBaseColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                break;
            case TYPE_ENCRYPT:
            case TYPE_DECRYPT:
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        return mDatabase.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ENCRYPT:
            case TYPE_DECRYPT:
                return YSFConstant.CONTENT_TYPE;
            case TYPE_ENCRYPT_ITEM:
            case TYPE_DECRYPT_ITEM:
                return YSFConstant.CONTENT_TYPE_ITEM;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        String table = uri.getPathSegments().get(0);
        long id = mDatabase.insert(table, null, values);
        if (id > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ENCRYPT:
            case TYPE_DECRYPT:
                count = mDatabase.delete(table, where, whereArgs);
                break;
            case TYPE_ENCRYPT_ITEM:
            case TYPE_DECRYPT_ITEM:
                String id = path.get(1);
                count = mDatabase.delete(table, YSFBaseColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());

        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String
            where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ENCRYPT:
            case TYPE_DECRYPT:
                count = mDatabase.update(table, values, where, whereArgs);
                break;
            case TYPE_ENCRYPT_ITEM:
            case TYPE_DECRYPT_ITEM:
                String id = path.get(1);
                count = mDatabase.update(table, values, YSFBaseColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());

        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }
}
